home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Text / Show / Less / less-252 / decode.c < prev    next >
C/C++ Source or Header  |  1994-10-15  |  14KB  |  639 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994  Mark Nudelman
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice in the documentation and/or other materials provided with 
  12.  *    the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  17.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  19.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  20.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  21.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  22.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  23.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  24.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Routines to decode user commands.
  30.  *
  31.  * This is all table driven.
  32.  * A command table is a sequence of command descriptors.
  33.  * Each command descriptor is a sequence of bytes with the following format:
  34.  *    <c1><c2>...<cN><0><action>
  35.  * The characters c1,c2,...,cN are the command string; that is,
  36.  * the characters which the user must type.
  37.  * It is terminated by a null <0> byte.
  38.  * The byte after the null byte is the action code associated
  39.  * with the command string.
  40.  * If an action byte is OR-ed with A_EXTRA, this indicates
  41.  * that the option byte is followed by an extra string.
  42.  *
  43.  * There may be many command tables.
  44.  * The first (default) table is built-in.
  45.  * Other tables are read in from "lesskey" files.
  46.  * All the tables are linked together and are searched in order.
  47.  */
  48.  
  49. #include "less.h"
  50. #include "cmd.h"
  51. #include "lesskey.h"
  52.  
  53. extern int erase_char, kill_char;
  54.  
  55. /*
  56.  * Command table is ordered roughly according to expected
  57.  * frequency of use, so the common commands are near the beginning.
  58.  */
  59. static char cmdtable[] =
  60. {
  61.     '\r',0,                A_F_LINE,
  62.     '\n',0,                A_F_LINE,
  63.     'e',0,                A_F_LINE,
  64.     'j',0,                A_F_LINE,
  65.     CONTROL('E'),0,            A_F_LINE,
  66.     CONTROL('N'),0,            A_F_LINE,
  67.     'k',0,                A_B_LINE,
  68.     'y',0,                A_B_LINE,
  69.     CONTROL('Y'),0,            A_B_LINE,
  70.     CONTROL('K'),0,            A_B_LINE,
  71.     CONTROL('P'),0,            A_B_LINE,
  72.     'J',0,                A_FF_LINE,
  73.     'K',0,                A_BF_LINE,
  74.     'Y',0,                A_BF_LINE,
  75.     'd',0,                A_F_SCROLL,
  76.     CONTROL('D'),0,            A_F_SCROLL,
  77.     'u',0,                A_B_SCROLL,
  78.     CONTROL('U'),0,            A_B_SCROLL,
  79.     ' ',0,                A_F_SCREEN,
  80.     'f',0,                A_F_SCREEN,
  81.     CONTROL('F'),0,            A_F_SCREEN,
  82.     CONTROL('V'),0,            A_F_SCREEN,
  83.     'b',0,                A_B_SCREEN,
  84.     CONTROL('B'),0,            A_B_SCREEN,
  85.     ESC,'v',0,            A_B_SCREEN,
  86.     'z',0,                A_F_WINDOW,
  87.     'w',0,                A_B_WINDOW,
  88.     'F',0,                A_F_FOREVER,
  89.     'R',0,                A_FREPAINT,
  90.     'r',0,                A_REPAINT,
  91.     CONTROL('R'),0,            A_REPAINT,
  92.     CONTROL('L'),0,            A_REPAINT,
  93.     ESC,'u',0,            A_UNDO_SEARCH,
  94.     'g',0,                A_GOLINE,
  95.     '<',0,                A_GOLINE,
  96.     ESC,'<',0,            A_GOLINE,
  97.     'p',0,                A_PERCENT,
  98.     '%',0,                A_PERCENT,
  99.     '{',0,                A_F_BRACKET|A_EXTRA,    '{','}',0,
  100.     '}',0,                A_B_BRACKET|A_EXTRA,    '{','}',0,
  101.     '(',0,                A_F_BRACKET|A_EXTRA,    '(',')',0,
  102.     ')',0,                A_B_BRACKET|A_EXTRA,    '(',')',0,
  103.     '[',0,                A_F_BRACKET|A_EXTRA,    '[',']',0,
  104.     ']',0,                A_B_BRACKET|A_EXTRA,    '[',']',0,
  105.     ESC,CONTROL('F'),0,        A_F_BRACKET,
  106.     ESC,CONTROL('B'),0,        A_B_BRACKET,
  107.     'G',0,                A_GOEND,
  108.     ESC,'>',0,            A_GOEND,
  109.     '>',0,                A_GOEND,
  110.     'P',0,                A_GOPOS,
  111.  
  112.     '0',0,                A_DIGIT,
  113.     '1',0,                A_DIGIT,
  114.     '2',0,                A_DIGIT,
  115.     '3',0,                A_DIGIT,
  116.     '4',0,                A_DIGIT,
  117.     '5',0,                A_DIGIT,
  118.     '6',0,                A_DIGIT,
  119.     '7',0,                A_DIGIT,
  120.     '8',0,                A_DIGIT,
  121.     '9',0,                A_DIGIT,
  122.  
  123.     '=',0,                A_STAT,
  124.     CONTROL('G'),0,            A_STAT,
  125.     ':','f',0,            A_STAT,
  126.     '/',0,                A_F_SEARCH,
  127.     '?',0,                A_B_SEARCH,
  128.     ESC,'/',0,            A_F_SEARCH|A_EXTRA,    '*',0,
  129.     ESC,'?',0,            A_B_SEARCH|A_EXTRA,    '*',0,
  130.     'n',0,                A_AGAIN_SEARCH,
  131.     ESC,'n',0,            A_T_AGAIN_SEARCH,
  132.     'N',0,                A_REVERSE_SEARCH,
  133.     ESC,'N',0,            A_T_REVERSE_SEARCH,
  134.     'm',0,                A_SETMARK,
  135.     '\'',0,                A_GOMARK,
  136.     CONTROL('X'),CONTROL('X'),0,    A_GOMARK,
  137.     'E',0,                A_EXAMINE,
  138.     ':','e',0,            A_EXAMINE,
  139.     CONTROL('X'),CONTROL('V'),0,    A_EXAMINE,
  140.     ':','n',0,            A_NEXT_FILE,
  141.     ':','p',0,            A_PREV_FILE,
  142.     ':','x',0,            A_INDEX_FILE,
  143.     '-',0,                A_OPT_TOGGLE,
  144.     ':','t',0,            A_OPT_TOGGLE|A_EXTRA,    't',0,
  145.     's',0,                A_OPT_TOGGLE|A_EXTRA,    'o',0,
  146.     '_',0,                A_DISP_OPTION,
  147.     '|',0,                A_PIPE,
  148.     'v',0,                A_VISUAL,
  149.     '!',0,                A_SHELL,
  150.     '+',0,                A_FIRSTCMD,
  151.  
  152.     'H',0,                A_HELP,
  153.     'h',0,                A_HELP,
  154.     'V',0,                A_VERSION,
  155.     'q',0,                A_QUIT,
  156.     ':','q',0,            A_QUIT,
  157.     ':','Q',0,            A_QUIT,
  158.     'Z','Z',0,            A_QUIT
  159. };
  160.  
  161. static char edittable[] =
  162. {
  163.     '\t',0,                EC_F_COMPLETE,    /* TAB */
  164.     '\17',0,        EC_B_COMPLETE,    /* BACKTAB */
  165.     '\14',0,        EC_EXPAND,    /* CTRL-L */
  166.     CONTROL('V'),0,        EC_LITERAL,    /* BACKSLASH */
  167.     CONTROL('A'),0,        EC_LITERAL,    /* BACKSLASH */
  168.        ESC,'l',0,        EC_RIGHT,    /* ESC l */
  169.     ESC,'h',0,        EC_LEFT,    /* ESC h */
  170.     ESC,'b',0,        EC_W_LEFT,    /* ESC b */
  171.     ESC,'w',0,        EC_W_RIGHT,    /* ESC w */
  172.     ESC,'i',0,        EC_INSERT,    /* ESC i */
  173.     ESC,'x',0,        EC_DELETE,    /* ESC x */
  174.     ESC,'X',0,        EC_W_DELETE,    /* ESC X */
  175.     ESC,'\b',0,        EC_W_BACKSPACE,    /* ESC BACKSPACE */
  176.     ESC,'0',0,        EC_HOME,    /* ESC 0 */
  177.     ESC,'$',0,        EC_END,        /* ESC $ */
  178.     ESC,'k',0,        EC_UP,        /* ESC k */
  179.     ESC,'j',0,        EC_DOWN,    /* ESC j */
  180.     ESC,'\t',0,        EC_B_COMPLETE    /* ESC TAB */
  181. };
  182.  
  183. /*
  184.  * Structure to support a list of command tables.
  185.  */
  186. struct tablelist
  187. {
  188.     struct tablelist *t_next;
  189.     char *t_start;
  190.     char *t_end;
  191. };
  192.  
  193. /*
  194.  * List of command tables and list of line-edit tables.
  195.  */
  196. static struct tablelist *list_fcmd_tables = NULL;
  197. static struct tablelist *list_ecmd_tables = NULL;
  198.  
  199.  
  200. /*
  201.  * Initialize the command lists.
  202.  */
  203.     public void
  204. init_cmds()
  205. {
  206.     add_fcmd_table(cmdtable, sizeof(cmdtable));
  207.     add_ecmd_table(edittable, sizeof(edittable));
  208. }
  209.  
  210. /*
  211.  * 
  212.  */
  213.     static int
  214. add_cmd_table(tlist, buf, len)
  215.     struct tablelist **tlist;
  216.     char *buf;
  217.     int len;
  218. {
  219.     register struct tablelist *t;
  220.  
  221.     /*
  222.      * Allocate a tablelist structure, initialize it, 
  223.      * and link it into the list of tables.
  224.      */
  225.     if ((t = (struct tablelist *) 
  226.             calloc(1, sizeof(struct tablelist))) == NULL)
  227.     {
  228.         return (-1);
  229.     }
  230.     t->t_start = buf;
  231.     t->t_end = buf + len;
  232.     t->t_next = *tlist;
  233.     *tlist = t;
  234.     return (0);
  235. }
  236.  
  237. /*
  238.  * 
  239.  */
  240.     public void
  241. add_fcmd_table(buf, len)
  242.     char *buf;
  243.     int len;
  244. {
  245.     if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
  246.         error("Warning: some commands disabled", NULL_PARG);
  247. }
  248.  
  249. /*
  250.  * 
  251.  */
  252.     public void
  253. add_ecmd_table(buf, len)
  254.     char *buf;
  255.     int len;
  256. {
  257.     if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
  258.         error("Warning: some edit commands disabled", NULL_PARG);
  259. }
  260.  
  261. /*
  262.  * Search a single command table for the command string in cmd.
  263.  */
  264.     public int
  265. cmd_search(cmd, table, endtable, sp)
  266.     char *cmd;
  267.     char *table;
  268.     char *endtable;
  269.     char **sp;
  270. {
  271.     register char *p;
  272.     register char *q;
  273.     register int a;
  274.  
  275.     for (p = table, q = cmd;  p < endtable;  p++, q++)
  276.     {
  277.         if (*p == *q)
  278.         {
  279.             /*
  280.              * Current characters match.
  281.              * If we're at the end of the string, we've found it.
  282.              * Return the action code, which is the character
  283.              * after the null at the end of the string
  284.              * in the command table.
  285.              */
  286.             if (*p == '\0')
  287.             {
  288.                 a = *++p & 0377;
  289.                 if (a == A_END_LIST)
  290.                 {
  291.                     /*
  292.                      * We get here only if the original
  293.                      * cmd string passed in was empty ("").
  294.                      * I don't think that can happen,
  295.                      * but just in case ...
  296.                      */
  297.                     return (A_UINVALID);
  298.                 }
  299.                 /*
  300.                  * Check for an "extra" string.
  301.                  */
  302.                 if (a & A_EXTRA)
  303.                 {
  304.                     *sp = ++p;
  305.                     a &= ~A_EXTRA;
  306.                 } else
  307.                     *sp = NULL;
  308.                 return (a);
  309.             }
  310.         } else if (*q == '\0')
  311.         {
  312.             /*
  313.              * Hit the end of the user's command,
  314.              * but not the end of the string in the command table.
  315.              * The user's command is incomplete.
  316.              */
  317.             return (A_PREFIX);
  318.         } else
  319.         {
  320.             /*
  321.              * Not a match.
  322.              * Skip ahead to the next command in the
  323.              * command table, and reset the pointer
  324.              * to the beginning of the user's command.
  325.              */
  326.             if (*p == '\0' && p[1] == A_END_LIST)
  327.             {
  328.                 /*
  329.                  * A_END_LIST is a special marker that tells 
  330.                  * us to abort the cmd search.
  331.                  */
  332.                 return (A_UINVALID);
  333.             }
  334.             while (*p++ != '\0') ;
  335.             if (*p & A_EXTRA)
  336.                 while (*++p != '\0') ;
  337.             q = cmd-1;
  338.         }
  339.     }
  340.     /*
  341.      * No match found in the entire command table.
  342.      */
  343.     return (A_INVALID);
  344. }
  345.  
  346. /*
  347.  * Decode a command character and return the associated action.
  348.  * The "extra" string, if any, is returned in sp.
  349.  */
  350.     static int
  351. cmd_decode(tlist, cmd, sp)
  352.     struct tablelist *tlist;
  353.     char *cmd;
  354.     char **sp;
  355. {
  356.     register struct tablelist *t;
  357.     register int action = A_INVALID;
  358.  
  359.     /*
  360.      * Search thru all the command tables.
  361.      * Stop when we find an action which is not A_INVALID.
  362.      */
  363.     for (t = tlist;  t != NULL;  t = t->t_next)
  364.     {
  365.         action = cmd_search(cmd, t->t_start, t->t_end, sp);
  366.         if (action != A_INVALID)
  367.             break;
  368.     }
  369.     return (action);
  370. }
  371.  
  372. /*
  373.  * Decode a command from the cmdtables list.
  374.  */
  375.     public int
  376. fcmd_decode(cmd, sp)
  377.     char *cmd;
  378.     char **sp;
  379. {
  380.     return (cmd_decode(list_fcmd_tables, cmd, sp));
  381. }
  382.  
  383. /*
  384.  * Decode a command from the edittables list.
  385.  */
  386.     public int
  387. ecmd_decode(cmd, sp)
  388.     char *cmd;
  389.     char **sp;
  390. {
  391.     return (cmd_decode(list_ecmd_tables, cmd, sp));
  392. }
  393.  
  394. #if USERFILE
  395.     static int
  396. gint(sp)
  397.     char **sp;
  398. {
  399.     int n;
  400.  
  401.     n = *(*sp)++;
  402.     n += *(*sp)++ * KRADIX;
  403.     return (n);
  404. }
  405.  
  406.     static int
  407. old_lesskey(buf, len)
  408.     char *buf;
  409.     int len;
  410. {
  411.     /*
  412.      * Old-style lesskey file.
  413.      * The file must end with either 
  414.      *     ...,cmd,0,action
  415.      * or  ...,cmd,0,action|A_EXTRA,string,0
  416.      * So the last byte or the second to last byte must be zero.
  417.      */
  418.     if (buf[len-1] != '\0' && buf[len-2] != '\0')
  419.         return (-1);
  420.     add_fcmd_table(buf, len);
  421.     return (0);
  422. }
  423.  
  424.     static int
  425. new_lesskey(buf, len)
  426.     char *buf;
  427.     int len;
  428. {
  429.     char *p;
  430.     register int c;
  431.     register int done;
  432.     register int n;
  433.  
  434.     /*
  435.      * New-style lesskey file.
  436.      * Extract the pieces.
  437.      */
  438.     if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
  439.         buf[len-2] != C1_END_LESSKEY_MAGIC ||
  440.         buf[len-1] != C2_END_LESSKEY_MAGIC)
  441.         return (-1);
  442.     p = buf + 4;
  443.     done = 0;
  444.     while (!done)
  445.     {
  446.         c = *p++;
  447.         switch (c)
  448.         {
  449.         case CMD_SECTION:
  450.             n = gint(&p);
  451.             add_fcmd_table(p, n);
  452.             p += n;
  453.             break;
  454.         case EDIT_SECTION:
  455.             n = gint(&p);
  456.             add_ecmd_table(p, n);
  457.             p += n;
  458.             break;
  459.         case END_SECTION:
  460.             done = 1;
  461.             break;
  462.         default:
  463.             free(buf);
  464.             return (-1);
  465.         }
  466.     }
  467.     return (0);
  468. }
  469.  
  470. /*
  471.  * Set up a user command table, based on a "lesskey" file.
  472.  */
  473.     public int
  474. lesskey(filename)
  475.     char *filename;
  476. {
  477.     register char *buf;
  478.     register POSITION len;
  479.     register long n;
  480.     register int f;
  481.  
  482.     /*
  483.      * Try to open the lesskey file.
  484.      */
  485.     f = open(filename, 0);
  486.     if (f < 0)
  487.         return (1);
  488.  
  489.     /*
  490.      * Read the file into a buffer.
  491.      * We first figure out the size of the file and allocate space for it.
  492.      * {{ Minimal error checking is done here.
  493.      *    A garbage .less file will produce strange results.
  494.      *    To avoid a large amount of error checking code here, we
  495.      *    rely on the lesskey program to generate a good .less file. }}
  496.      */
  497.     len = filesize(f);
  498.     if (len == NULL_POSITION || len < 3)
  499.     {
  500.         /*
  501.          * Bad file (valid file must have at least 3 chars).
  502.          */
  503.         close(f);
  504.         return (-1);
  505.     }
  506.     if ((buf = (char *) calloc((int)len, sizeof(char))) == NULL)
  507.     {
  508.         close(f);
  509.         return (-1);
  510.     }
  511.     if (lseek(f, (off_t)0, 0) == BAD_LSEEK)
  512.     {
  513.         free(buf);
  514.         close(f);
  515.         return (-1);
  516.     }
  517.     n = read(f, buf, (unsigned int) len);
  518.     close(f);
  519.     if (n != len)
  520.     {
  521.         free(buf);
  522.         return (-1);
  523.     }
  524.  
  525.     /*
  526.      * Figure out if this is an old-style (before version 241)
  527.      * or new-style lesskey file format.
  528.      */
  529.     if (buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC ||
  530.         buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC)
  531.         return (old_lesskey(buf, (int)len));
  532.     return (new_lesskey(buf, (int)len));
  533. }
  534.  
  535. /*
  536.  * Add the standard lesskey file "$HOME/.less"
  537.  */
  538.     public void
  539. add_hometable()
  540. {
  541.     char *filename;
  542.     PARG parg;
  543.  
  544.     filename = homefile(LESSKEYFILE);
  545.     if (filename == NULL)
  546.         return;
  547.     if (lesskey(filename) < 0)
  548.     {
  549.         parg.p_string = filename;
  550.         error("Cannot use lesskey file \"%s\"", &parg);
  551.     }
  552.     free(filename);
  553. }
  554. #endif
  555.  
  556. /*
  557.  * See if a char is a special line-editing command.
  558.  */
  559.     public int
  560. editchar(c, flags)
  561.     int c;
  562.     int flags;
  563. {
  564.     int action;
  565.     int nch;
  566.     char *s;
  567.     char usercmd[MAX_CMDLEN+1];
  568.     
  569.     /*
  570.      * An editing character could actually be a sequence of characters;
  571.      * for example, an escape sequence sent by pressing the uparrow key.
  572.      * To match the editing string, we use the command decoder
  573.      * but give it the edit-commands command table
  574.      * This table is constructed to match the user's keyboard.
  575.      */
  576.     if (c == erase_char)
  577.         return (EC_BACKSPACE);
  578.     if (c == kill_char)
  579.         return (EC_LINEKILL);
  580.         
  581.     /*
  582.      * Collect characters in a buffer.
  583.      * Start with the one we have, and get more if we need them.
  584.      */
  585.     nch = 0;
  586.     do {
  587.           if (nch > 0)
  588.             c = getcc();
  589.         usercmd[nch] = c;
  590.         usercmd[nch+1] = '\0';
  591.         nch++;
  592.         action = ecmd_decode(usercmd, &s);
  593.     } while (action == A_PREFIX);
  594.     
  595.     if (flags & EC_NOHISTORY) 
  596.     {
  597.         /*
  598.          * The caller says there is no history list.
  599.          * Reject any history-manipulation action.
  600.          */
  601.         switch (action)
  602.         {
  603.         case EC_UP:
  604.         case EC_DOWN:
  605.             action = A_INVALID;
  606.             break;
  607.         }
  608.     }
  609.     if (flags & EC_NOCOMPLETE) 
  610.     {
  611.         /*
  612.          * The caller says we don't want any filename completion cmds.
  613.          * Reject them.
  614.          */
  615.         switch (action)
  616.         {
  617.         case EC_F_COMPLETE:
  618.         case EC_B_COMPLETE:
  619.         case EC_EXPAND:
  620.             action = A_INVALID;
  621.             break;
  622.         }
  623.     }
  624.     if ((flags & EC_PEEK) || action == A_INVALID)
  625.     {
  626.         /*
  627.          * We're just peeking, or we didn't understand the command.
  628.          * Unget all the characters we read in the loop above.
  629.          * This does NOT include the original character that was 
  630.          * passed in as a parameter.
  631.          */
  632.         while (nch > 1) {
  633.             ungetcc(usercmd[--nch]);
  634.         }
  635.     }
  636.     return action;
  637. }
  638.  
  639.